package com.eig.web.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eig.common.entity.system.TbMember;
import com.eig.common.util.JWTUtil;
import com.eig.common.vo.Result;
import com.eig.web.dao.TbMemberDao;
import com.eig.web.service.TbMemberService;
import com.eig.web.util.mmm;
import com.eig.web.vo.Qianvo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 会员表(TbMember)表服务实现类
 *
 * @author makejava
 * @since 2022-12-25 13:45:52
 */
@Service("tbMemberService")
public class TbMemberServiceImpl implements TbMemberService {
    @Resource
    private TbMemberDao tbMemberDao;


    @Override
    public Result qianlogin(Qianvo qianvo) {
        System.out.println(qianvo.toString());
        QueryWrapper<TbMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", qianvo.getMobile());
        wrapper.eq("is_disabled",0);
        TbMember tbMember = tbMemberDao.selectOne(wrapper);
        if (tbMember != null) {
            Map tokenMap = new HashMap();
            tokenMap.put("mobile",tbMember.getMobile());
            tokenMap.put("password", tbMember.getPassword());
            tokenMap.put("type", tbMember.getIsType());
            String s = mmm.makePasswordSalt(qianvo.getQianPassword(), tbMember.getSalt());
            if (tbMember.getPassword().equals(s)) {
                Map map = new HashMap();
                String homeToken = JWTUtil.createJWT(tokenMap);
                map.put( "homeToken",homeToken);
                map.put( "mobile" , tbMember.getMobile());
                map.put( "memberId" , tbMember.getId());
                if (tbMember.getIsType().equals(1)){
                    map.put( "isTeacher","1");
                    return new Result(2000,"登录成功",map);
                }
                map.put("isTeacher","0");
                return new Result(2000,"登录成功",map);
            } else {
                return new Result(500, "密码错误");
            }
        } else {
            return new Result(500, "手机号错误");
        }
    }


}
